package logic

import (
	"8-dshop/api/internal/utils"
	"8-dshop/pkg"
	"8-dshop/shoprpc/shoprpc"
	"context"
	"strconv"

	"8-dshop/api/internal/svc"
	"8-dshop/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.ShopRpc.Login(l.ctx, &shoprpc.LoginRequest{
		Mobile:   req.Mobie,
		Password: req.Password,
	})
	if err != nil {
		return utils.ReturnResponse(err.Error(), 0, nil)
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(login.UserId, 10))
	if err != nil {
		return utils.ReturnResponse(err.Error(), 0, nil)
	}
	return utils.ReturnResponse("登录成功", 200, handler)
}
